package com.honeywen.mymvc.security;

import com.honeywen.mymvc.entity.ActiveUser;
import com.honeywen.mymvc.entity.User;

import java.util.List;

/**
 * @author wangwei
 * @date 2019/1/18 下午5:53
 */
public class MyHelloRealm extends HelloRealm {


    @Override
    public ActiveUser authenticate(String userCode, String password) throws Exception {

        // find from db
        User user = new User();
        if (user == null) {
            throw new Exception("no account");
        }


        String pasword_db = user.getPassword();
        // compare
        if (!password.equals(pasword_db)) {
            throw new Exception("wrong password");
        }
        // get user menus and permissions
        List<ActiveUser.SysPermission> menus = findMenuListByUserId(user.getId());
        List<ActiveUser.SysPermission> permissions = findPermissionListByUserId(user.getId());

        ActiveUser activeUser = new ActiveUser();
        activeUser.setUserid(user.getId());
        activeUser.setUsercode(userCode);
        activeUser.setUsername(user.getName());

        activeUser.setMenus(menus);
        activeUser.setPermissions(permissions);

        return activeUser;

    }

    private List<ActiveUser.SysPermission> findPermissionListByUserId(Integer id) {
        return null;
    }

    private List<ActiveUser.SysPermission> findMenuListByUserId(Integer id) {
        return null;
    }
}
